﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace LudumDare25Project.Objects
{
    internal class Spaceship
    {
        public Vector2 Position = Vector2.Zero;
        public Vector2 velocity = Vector2.Zero;
        public Vector2 acceleration = Vector2.Zero;

        public float Radius;

        private Texture2D shipTexture = null;
        private Texture2D shieldTexture = null;

        public float HullHealth = 100.0f;
        public float Invulnerable = 0.0f;
        public float Shield = 100.0f;

        public bool ShieldActive = false;

        public const int MaxAmmo = 250;
        public int Ammo = MaxAmmo;

        public const int MaxMissiles = 6;
        public int Missiles = MaxMissiles;

        public float enemyShootTimer = 0.0f;

        public bool Wavy = false;
        private float timer = 0.0f;

        private Flame flame;

        public Spaceship(ContentManager content, Texture2D texture, Texture2D shieldTexture, Vector2 flameOffset)
        {
            shipTexture = texture;
            this.shieldTexture = shieldTexture;

            Radius = (float)Math.Sqrt((texture.Width * texture.Width) + (texture.Height * texture.Height));
            Radius *= 0.3f;

            flame = new Flame(content, flameOffset, 1.0f);
        }

        public void HandleInput(InputManager input, Camera cam)
        {
            if (Invulnerable <= 0.0f)
            {
                acceleration = Vector2.Zero;

                if (input.KeyStates[(int)Keys.W].ThisFrame || input.KeyStates[(int)Keys.Up].ThisFrame)
                    acceleration.Y -= 100.0f;
                else if (input.KeyStates[(int)Keys.S].Down || input.KeyStates[(int)Keys.Down].ThisFrame)
                    acceleration.Y += 100.0f;
            }
        }

        public void Update(float dt)
        {
            flame.Update(dt);

            velocity += (acceleration * 20.0f * dt);

            if (Wavy)
            {
                timer += dt;
                velocity.Y = MathHelper.Lerp(-300.0f, 300.0f, (float)Math.Sin(timer));
            }

            Invulnerable -= dt;
            Position += velocity * dt;
        }

        public void Render(SpriteBatch sprites)
        {
            float rot = !Wavy ? 0.0f : (float)Math.Atan2(velocity.Y, velocity.X);

            if (velocity.X < -10.0f)
                rot += MathHelper.Pi;

            flame.Draw(sprites, Position, rot);

            sprites.Draw(
                shipTexture,
                Position,
                null,
                Invulnerable <= 0.0f ? Color.White : Color.Red,
                !Wavy ? 0.0f : (float)Math.Atan2(velocity.Y, velocity.X) + MathHelper.Pi,
                new Vector2(shipTexture.Width, shipTexture.Height) * 0.5f,
                1.0f,
                SpriteEffects.None,
                0.0f);

            if (shieldTexture != null && (Invulnerable > 0.0f || ShieldActive))
            {
                sprites.Draw(
                    shieldTexture,
                    Position,
                    null,
                    Invulnerable <= 0.0f ? Color.White : Color.Red,
                    0.0f,
                    new Vector2(shieldTexture.Width, shieldTexture.Height) * 0.5f,
                    1.0f,
                    SpriteEffects.None,
                    0.0f);
            }
        }
    }
}